package o2o.service.delivery;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinyouapp.common.utils.common.HttpClientUtil;
import o2o.service.delivery.to.*;
import org.apache.commons.lang.StringUtils;
import org.apache.http.util.TextUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Jahnke【1029777564@qq.com】
 * @ClassName DeliveryOrderSyncService
 * @Description: 订单配送数据发送到第三方（第三方平台负责配送）
 * @date 2020/1/7 9:43
 * @Version 1.0
 */
public class DeliveryOrderSyncService {

    private DeliveryOrderSyncService() {
    }

    enum Singleton {
        INSTANCE;
        private DeliveryOrderSyncService single;

        private Singleton() {
            single = new DeliveryOrderSyncService();
        }

        private DeliveryOrderSyncService getInstance() {
            return single;
        }
    }

    public static DeliveryOrderSyncService getInstance() {
        return Singleton.INSTANCE.single;
    }


    public final static String DELIVERY_PLATEFORM_DADA = "dada";

    private static String DELIVERY_URL_DADA_SHOP = "https://jycloud.jinyouapp.com/api/delivery/shop/info/add-update";               // 门店添加/更新
    private static String DELIVERY_URL_DADA_ORDER_MONEY = "https://jycloud.jinyouapp.com/api/delivery/order/info/delivery-money";   // 获取配送费
    private static String DELIVERY_URL_DADA_ORDER_SEND = "https://jycloud.jinyouapp.com/api/delivery/order/info/place";             // 发送配送单
    private static String DELIVERY_URL_DADA_ORDER_CANCEL = "https://jycloud.jinyouapp.com/api/delivery/order/info/cancel";          // 取消配送单
    private static String DELIVERY_URL_DADA_ORDER_QUERY = "https://jycloud.jinyouapp.com/api/delivery/order/info/query";          // 取消配送单

    // 店铺注册或者信息修改
    public void addOrUpdateDeliverShop(String deliveryPlate, DeliveryShopTO to) throws DeliveryException {
        if (StringUtils.isNotEmpty(deliveryPlate)) {
            if (deliveryPlate.equals(DELIVERY_PLATEFORM_DADA)) {
                BasicDeliveryTO deliveryTO = this.post(DELIVERY_URL_DADA_SHOP, JSON.toJSONString(to), deliveryPlate, BasicDeliveryTO.class);
                if (deliveryTO.getState().equals(0)) {
                    throw new DeliveryException(deliveryTO.getError());
                } else {
                    return;
                }
            }
        }
        throw new DeliveryException("店铺注册/修改出现错误!");
    }

    // 计算配送费
    public DeliveryFeeTO getDeliverMoney(String deliveryPlate, DeliveryOrderTO deliveryOrderTO) throws DeliveryException {
        if (StringUtils.isNotEmpty(deliveryPlate)) {
            DeliveryFeeTO to = this.post(DELIVERY_URL_DADA_ORDER_MONEY, JSON.toJSONString(deliveryOrderTO), deliveryPlate, DeliveryFeeTO.class);
            return to;
        }
        throw new DeliveryException("配送费计算错误错!");
    }

    // 配送订单发出
    public DeliveryFeeTO sendDeliverData(String deliveryPlate, DeliveryOrderTO deliveryOrderTO) throws DeliveryException {
        if (StringUtils.isNotEmpty(deliveryPlate)) {
            DeliveryFeeTO to = this.post(DELIVERY_URL_DADA_ORDER_SEND, JSON.toJSONString(deliveryOrderTO), deliveryPlate, DeliveryFeeTO.class);
            if (to.getState().equals(0)) {
                throw new DeliveryException(to.getError());
            }
            return to;
        }
        throw new DeliveryException("配送出错!");
    }

    // 取消配送订单
    public void cancelDeliveryOrder(String deliveryPlate, String sysCustomer, String orderNo, String reason) throws DeliveryException {
        if (StringUtils.isNotEmpty(deliveryPlate)) {
            Map<String, String> params = new HashMap<>();
            params.put("sysCustomer", sysCustomer);
            params.put("orderNo", orderNo);
            if (TextUtils.isEmpty(reason)) {
                reason = "不要了！";
            }
            params.put("reason", reason);
            params.put("plate", StringUtils.isEmpty(deliveryPlate) ? "plate" : deliveryPlate);

            String json = HttpClientUtil.sendPost(DELIVERY_URL_DADA_ORDER_CANCEL, params);
            BasicDeliveryTO deliveryTO = this.deliveryResult(json, BasicDeliveryTO.class);
            if (deliveryTO.getState().equals(0) && !deliveryTO.getError().contains("已取消")) {
                throw new DeliveryException(deliveryTO.getError());
            } else {
                return;
            }
        } else {
            throw new DeliveryException("取消配送出错!");
        }
    }

    // 解析订单状态(服务器回调-解析)
    public DeliveryOrderStateTO getDeliveryState(String json) {
        if (StringUtils.isNotEmpty(json)) {
            DeliveryOrderStateTO to = JSONObject.parseObject(json, DeliveryOrderStateTO.class);
            return to;
        }
        return null;
    }

    // 查询配送单的状态
    public DeliveryOrderStateTO queryDeliveryState(String deliveryPlate, String sysCustomer, String orderNo) throws DeliveryException {
        if (StringUtils.isNotEmpty(deliveryPlate)) {
            Map<String, String> params = new HashMap<>();
            params.put("sysCustomer", sysCustomer);
            params.put("plate", StringUtils.isEmpty(deliveryPlate) ? "plate" : deliveryPlate);
            params.put("orderNo", orderNo);

            String json = HttpClientUtil.sendPost(DELIVERY_URL_DADA_ORDER_QUERY, params);
            DeliveryOrderStateTO deliveryTO = this.deliveryResult(json, DeliveryOrderStateTO.class);
            if (deliveryTO.getState().equals(0)) {
                throw new DeliveryException(deliveryTO.getError());
            }
            return deliveryTO;
        }
        throw new DeliveryException("查询配送信息错误!");
    }

    // 数据发送
    private <T> T post(String url, String datas, String deliveryPlate, Class<T> c) throws DeliveryException {
        Map<String, String> params = new HashMap<>();
        params.put("json", datas);
        params.put("plate", deliveryPlate);
        String json = HttpClientUtil.sendPost(url, params);
        return this.deliveryResult(json, c);
    }

    // 接单的数据解析
    private <T> T deliveryResult(String json, Class<T> c) throws DeliveryException {
        JSONObject object = JSONObject.parseObject(json);
        if (object.getString("status").equals("1")) {
            return object.getObject("info", c);
        }
        throw new DeliveryException(object.containsKey("error") ? object.getString("error") : "解析出现错误");
    }
}
